package com.sam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginGoogle extends ActionSupport {

	private static final long serialVersionUID = -3698687828246029357L;
	private String url;

	public String execute() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserService userService = UserServiceFactory.getUserService();
		HttpServletRequest request = ServletActionContext.getRequest();
		String thisURL = request.getRequestURI();
		if (request.getUserPrincipal() != null) {
			session.put("usuario", userService.getCurrentUser().getEmail());
			if(userService.isUserAdmin()){
				return "ADMIN"; 
			}
			return "loggin";
		} else {
			setUrl(userService.createLoginURL(thisURL));
			return "notLoggin";
		}

	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
